 package com.zu.commu.auto1_0.protocol.chargeTimes;

 import com.zu.commu.auto1_0.DataType1_0;
import com.zu.commu.auto1_0.protocol.CheckData;
import com.zu.commu.auto1_0.protocol.Util;
import com.zu.commu.exception.CommuException;
import com.zu.commu.interf.Data;

public class SetChargeTimesDataProtocol
{
  public Data analyseReportData(byte[] b)
    throws Exception
  {
    new CheckData().checkCrc(b, 4, 14, 15);
    Data d = new Data();
    getMeterNo(b, d);
    getChargeTimes(b, d);
    getChargeSucc(b, d);
    d.setDataType(DataType1_0.CHARGETIMES.toInt()+"");
    return d;
  }

  private void getMeterNo(byte[] b, Data d)
    throws Exception
  {
    try
    {
      Util u = new Util();
      d.setMeterNo(u.byte2Hex(b, 4, 9));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取测控器地址号时出错！" +
        e.getMessage(), null);
    }
  }

  private void getChargeTimes(byte[] b, Data d)
    throws Exception
  {
    try
    {
      Util u = new Util();
      d.setChargeTimes(Integer.valueOf(u.string2Int(u.byte2Hex(b, 12, 13))));
    } catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取充值次数时出错！" +
        e.getMessage(), null);
    }
  }

  private void getChargeSucc(byte[] b, Data d)
    throws Exception
  {
    try
    {
      Util u = new Util();
      String r = u.byte2Hex(b, 14, 14);
      if (r.equalsIgnoreCase("00")) {
        d.setOrderSucc("0");
      }
      if (r.equalsIgnoreCase("ff"))
        d.setOrderSucc("1");
    }
    catch (Exception e) {
      throw new CommuException().setMessage("从上报数据中读取充值成功与否标识时出错！" +
        e.getMessage(), null);
    }
  }
}
